/*
 * Getrap
 * Copyright 2008(C) Grzegorz 'milyges' Glinski
 *
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
*/
#include <config.h>
#include <QtDebug>
#include <cstdlib>

QList<struct configItem> Config::mData;

QString Config::getPath(void)
{
 #ifdef Q_WS_X11
 /** Na systemach z rodziny UNIX plik konfiguracyjny trzymamy w katalogu domowym użytkownika */
 return QString(getenv("HOME")) + "/.getrap";
 #else
 #ifdef Q_WS_WIN
 /** Na systemach Microsoft Windows a katalogu Danych Aplikacji */
 return QString(getenv("APPDATA")) + "/getrap.ini";
 #else
 #error "Unsupported platform"
 #endif /* Q_WS_WIN */
 #endif /* Q_WS_X11 */
}

bool Config::load()
{
 QFile * f;
 QByteArray line;
 QStringList tmp;
 f = new QFile(getPath());
 /* Sprawdzamy czy plik istnieje */
 if (!f->exists())
 {
  qDebug() << "Config::load(): Config file not exist";
  delete f;
  return false;
 }
 /* Otwieramy w trybie tylko do odczytu */
 f->open(QIODevice::ReadOnly | QIODevice::Text);
 /* Czytamy linia po lini */
 while (!f->atEnd())
 {
  line = f->readLine();
  tmp = QString(line).split("=", QString::SkipEmptyParts);
  /* Pomijamy niepoprawne linie */
  if (tmp.size() != 2)
     continue;
  /* Dodajemy wpisy do pamięci */
  setValue(tmp[0].trimmed(), tmp[1].trimmed());
 }
 /* Zamykamy plik */
 f->close();
 delete f;
 return true;
}

bool Config::save()
{
 int i;
 QFile * f;
 QString line;
 /* Probojemy otworzyc plik */
 f = new QFile(getPath());
 if (!f->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
 {
  delete f;
  return false;
 }
 f->write("# DO NOT EDIT THIS FILE\n");
 /* Zapisujemy dane z pamieci do pliku */
 for(i=0;i<mData.size();i++)
 {
  line = mData[i].name + "=" + mData[i].value + "\n";
  f->write(line.toUtf8());
 }
 /* Zamykamy plik */
 f->close();
 delete f;
 return true;
}

QString Config::getValue(QString name)
{
 int i;
 /* Szukamy w tablicy konfiguracji wpisu o podanej nazwie */
 for(i=0;i<mData.size();i++)
 {
  if (mData[i].name == name)
     return mData[i].value;
 }
 /* Jezeli nie istnieje zwracamy pusty string */
 return QString("");
}

void Config::setValue(QString name, QString value)
{
 int i;
 struct configItem newItem;
 /* Jezeli wpis istanieje to go aktualizujemy */
 for(i=0;i<mData.size();i++)
 {
  if (mData[i].name == name)
  {
   mData[i].value = value;
   return;
  }
 }
 /* Jezeli wpis nie istnieje dodajemy nowy */
 newItem.name = name;
 newItem.value = value;
 mData.push_back(newItem);
}
